﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
 * 宝箱类
 */

public class Treasure : MonoBehaviour
{
    public GameObject pickup_effect;//道具效果

    public AudioClip m_SEPicked;//拾取时的音效

    public AudioClip m_SEAppear;//出现时的音效

    private PlayerControl player;

    public float m_Disappear = 100f;
    //Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindWithTag("Player").GetComponent<PlayerControl>();
        Destroy(gameObject, m_Disappear);
    }

    //Update is called once per frame
    void Update()
    {

    }

    enum RANDOM_LIST
    {
        RECOVERY,       //恢复生命
        UPSPEED,        //增加移动速度
        GHOSTDOWN,      //幽灵移速减慢
        UPGRADE,        //增加得分
        INVINCIBLE,     //无敌
        DECREASEHP,     //减少生命
        RANDOMKILL      //随机杀死幽灵
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.GetComponent<PlayerControl>())
        {
            RandomState();
            for (int i = 0; i < transform.childCount; i++)
            {
                transform.GetChild(i).GetComponent<ParticleSystem>().Play();
                if(i==2) transform.GetChild(i).GetComponent<ParticleSystem>().Stop();
            }
            other.SendMessage("PlayPartical",0);
            Destroy(gameObject, 5);
        }
    }

    public void RandomState()
    {
        System.Random ran = new System.Random();
        RANDOM_LIST[] list = Enum.GetValues(typeof(RANDOM_LIST)) as RANDOM_LIST[];
        GameObject[] objs = GameObject.FindGameObjectsWithTag("ghost");
        RANDOM_LIST temp = list[ran.Next(0, list.Length)];
        switch (temp)
        {
            case RANDOM_LIST.DECREASEHP:
                player.DecreaseHp();
                break;
            case RANDOM_LIST.GHOSTDOWN:
                foreach (GameObject obj in objs)
                    obj.GetComponent<MonsterCtrl>().DecreaseSpeed();
                break;
            case RANDOM_LIST.INVINCIBLE:
                player.Invincible();
                break;
            case RANDOM_LIST.RANDOMKILL:
                if (objs.Length > 0)
                {
                    Destroy(objs[UnityEngine.Random.Range(0, objs.Length)]);
                    GameObject.Find("GameControllor").SendMessage("MonsterDead",0);
                }

                break;
            case RANDOM_LIST.RECOVERY:
                player.Recovery();
                break;
            case RANDOM_LIST.UPGRADE:
                break;
            case RANDOM_LIST.UPSPEED:
                player.Accelerate();
                break;
        }
    }
}
